<template>
  <div>
    <div>
      <span class="basic">货代管理</span>
    </div>
    <!--搜索-->
    <div class="toolbar">
      <el-text class="mx-1">货代公司名称:&nbsp;</el-text>
      <el-input
        v-model="data.companyName"
        placeholder="请输入"
        style="width: 200px; margin-right: 10px"
      />
      <el-text class="mx-1">联系方式:&nbsp;</el-text>
      <el-input
        v-model="data.mobile"
        placeholder="请输入"
        style="width: 200px; margin-right: 10px"
      />
      <el-text class="mx-1">邮箱:&nbsp;</el-text>
      <el-input
        v-model="data.email"
        placeholder="请输入"
        style="width: 200px; margin-right: 10px"
      />
      <el-button type="primary" @click="getList">查询</el-button>
      <el-button type="primary" @click="reset">重置</el-button>
    </div>
    <div class="add">
      <el-button type="primary" @click="goToAddForw">添加</el-button>
    </div>
    <!-- 表格数据 -->
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="ID" align="center" />
      <el-table-column prop="companyName" label="货代公司名称" align="center" />
      <el-table-column prop="name" label="联系人" align="center" />
      <el-table-column prop="mobile" label="联系方式" align="center" />
      <el-table-column prop="email" label="邮箱" align="center" />
      <el-table-column prop="createTime" label="创建时间" align="center" />
      <el-table-column label="操作" align="center" width="300px">
        <template #default="{ row }">
          <el-button @click="goToForw(row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="background-color: #fff">
      <el-pagination
        layout="prev, pager, next,sizes,total,jumper"
        :total="total"
        @current-change="chpage"
        @size-change="chsize"
      />
    </div>
    <AddForwarder
      :visible="isShow"
      @close="isShow = false"
      @refresh="getList()"
    />
    <updateForwarder
      :visible="isShow2"
      :fid="sid"
      @close2="isShow2 = false"
      @refresh2="getList()"
    />
  </div>
</template>

<script setup>
import { ref, onBeforeMount } from "vue";
import { getForw } from "@/api/system/forw.js";
import AddForwarder from "./AddForwarder.vue";
import updateForwarder from "./updateForwarder.vue";

let isShow = ref(false);
let isShow2 = ref(false);
let sid = ref(0);
const tableData = ref([]);
const page = ref(1);
const pageSize = ref(10);
const total = ref(0); // 总记录数
const data = ref({
  companyName: "",
  mobile: "",
  email: ""
});

// 分页查询员工列表
async function getList() {
  // 准备查询参数
  let params = {
    page: page.value,
    pageSize: pageSize.value,
    companyName: data.value.companyName,
    mobile: data.value.mobile,
    email: data.value.email
  };

  // 向后端发起请求，传递搜索参数
  let resu = await getForw(params);

  if (resu.code == 200) {
    tableData.value = resu.data.list;
    page.value = resu.data.page;
    pageSize.value = resu.data.pageSize;
    total.value = resu.data.total;

    // 遍历列表并格式化 enquiryTime
    tableData.value = tableData.value.map(item => {
      if (item.createTime) {
        item.createTime = new Date(item.createTime).toLocaleString(); // 格式化时间
      }
      return item;
    });
  }
}

// 重置表单
function reset() {
  data.value.companyName = "";
  data.value.email = "";
  data.value.mobile = "";
  getList(); // 重置后重新加载列表
}

// 分页处理
function chpage(pg) {
  page.value = pg;
  getList();
}

function chsize(ps) {
  pageSize.value = ps;
  page.value = 1;
  getList();
}

// 点击编辑按钮时跳转
function goToForw(row) {
  isShow2.value = true;
  sid.value = row.id;
}
// 点击添加钮时跳转
function goToAddForw() {
  isShow.value = true;
}
// 初始化加载列表
onBeforeMount(() => {
  getList();
});
</script>

<style scoped>
.basic {
  font-size: 24px;
  font-weight: bold;
  color: #0054a6;
  display: inline-block;
  border-style: none;
}
</style>
